package com.wangy.service.impl;

import com.wangy.bean.GeneralStatistic;
import com.wangy.bean.Page;
import com.wangy.bean.PagePath;
import com.wangy.bean.TrafficStatistic;
import com.wangy.mapper.StatisticClickhouseMapper;
import com.wangy.mapper.StatisticMySQLMapper;
import com.wangy.service.StatisticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author yyzb
 * @date 2024/12/29 19:09
 */
@Service
public class StatisticServiceImpl implements StatisticService {

    @Autowired
    StatisticMySQLMapper statisticMySQLMapper;

    @Autowired
    StatisticClickhouseMapper statisticClickhouseMapper;

    @Override
    public BigDecimal getGMV(String date) {
        return statisticClickhouseMapper.getGMV(date);
    }

    @Override
    public List<GeneralStatistic> getProvinceStatistics(String date) {
        return statisticClickhouseMapper.getProvinceStatistics(date);
    }

    @Override
    public List<GeneralStatistic> getKeywordStatistics(String date) {
        return statisticClickhouseMapper.getKeywordStatistics(date);
    }

    @Override
    public List<GeneralStatistic> getProductStatisticsGroupByCategory3(String date) {
        return statisticClickhouseMapper.getProductStatisticsGroupByCategory3(date);
    }

    @Override
    public List<GeneralStatistic> getProductStatsByTrademark(String date) {
        return statisticClickhouseMapper.getProductStatsByTrademark(date);
    }

    @Override
    public Page<TrafficStatistic> getTrafficStatistics(Integer page, Integer limit, String dt, Integer recentDays) {
        Page<TrafficStatistic> trafficStatisticPage = new Page<>();
        Integer offset = page-1;
        List<TrafficStatistic> trafficStatisticList = statisticMySQLMapper.getTrafficStatistics(offset, limit, dt, recentDays);
        trafficStatisticPage.setSize(limit);
        trafficStatisticPage.setCurrent(page);
        trafficStatisticPage.setTotal(trafficStatisticList.size());
        trafficStatisticPage.setRecords(trafficStatisticList);

        return trafficStatisticPage;
    }

    // 点集和边集 点集里面是("name", name) 边集里面是PagePath列表
    @Override
    public Map<String, Object> getPagePathCount(TrafficStatistic trafficStatistic) {
        List<PagePath> pagePaths = statisticMySQLMapper.getPagePathCount(trafficStatistic);
        HashSet<String> nodeSet = new HashSet<>();
        for (PagePath pagePath: pagePaths){
            nodeSet.add(pagePath.getSource());
            nodeSet.add(pagePath.getTarget());
        }
        List<Map<String, String>> nodeMapList = new ArrayList<>();
        for (String node: nodeSet){
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("name", node);
            nodeMapList.add(hashMap);
        }
        HashMap<String, Object> rsMap = new HashMap<>();
        rsMap.put("nodeData", nodeMapList);
        rsMap.put("linksData", pagePaths);
        return rsMap;
    }
}
